package com.study.test.demo.proxy.jdkdynamiproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author hupengming
 * @ClassName JdkProxy
 * @Date 2023-03-09  16:08
 * @Version 1.0
 */
public class JdkProxy implements InvocationHandler {

    private Restaurant restaurant;

    public Restaurant instance(Restaurant target) {
        this.restaurant = target;
        Class<? extends Restaurant> aClass = restaurant.getClass();
        return (Restaurant) Proxy.newProxyInstance(aClass.getClassLoader(), aClass.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(this.restaurant, args);
        after();
        return invoke;
    }

    private void after() {
        System.out.println("通知骑手关单");
    }

    private void before() {
        System.out.println("通知骑手接单");
    }
}
